//
//  MYGSubmitAnswerViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/25.
// 提交回答

import UIKit
import QMUIKit
import IQKeyboardManagerSwift

class MYGSubmitAnswerViewController: MYGBaseController {
    var model: MYGCommunityAnswerDetailModel? {
        didSet {
            
            guard let model = model else {return}
            
            let item = MYGCommunityDetailModel()
            let communities_patient = MYGCommunityDetailCommunitiesPatientModel()
            
            communities_patient.title = model.title
            
            let patient = MYGCommunityDetailPatientModel()
            patient.name = model.patient?.name
            patient.avatar = model.patient?.avatar
            
            communities_patient.patient = patient
            item.communities_patient = communities_patient
            infoView.model = item
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
        cardContentView.isHidden = false
        //answerView.model = model
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        IQKeyboardManager.shared.enable = false
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        IQKeyboardManager.shared.enable = true
    }
    
    // MARK: - UI
    private func configUI() {
        title = "回答"
        view.backgroundColor = .white
        view.addSubview(cardContentView)
       // scrollView.addSubview(cardContentView)
        cardContentView.addSubviews([
            infoView,
            sperator,
            inputTextView
        ])
        
        view.addSubview(answerButton)
//        scrollView.snp.makeConstraints { make in
//            make.top.equalToSuperview()
//            make.left.right.bottom.equalToSuperview()
//        }
//        
        cardContentView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.left.right.equalToSuperview().inset(15)
            make.width.equalTo(screenWidth-30)
            make.bottom.equalToSuperview().offset(-113)
        }
        
        infoView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
        }
        
        sperator.snp.makeConstraints { make in
            make.bottom.equalTo(infoView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        inputTextView.snp.makeConstraints { make in
            make.top.equalTo(sperator.snp.bottom).offset(10)
            make.left.right.bottom.equalToSuperview().inset(15)
        }
        
        answerButton.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview().inset(28)
            make.height.equalTo(39)
        }
    }
 
    // MARK: - lazy load
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    
    private lazy var cardContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 5
        view.clipsToBounds = false
        view.layerShadowColor = .rgba(0, 0, 0, 0.1)
        view.layerShadowOffset = CGSize(width: 0, height: 0)
        view.layerShadowOpacity = 1
        view.layerShadowRadius = 5
        view.isHidden = true
        return view
    }()
    
    private lazy var infoView: MYGCommunityQuestionInfoView = {
        let view = MYGCommunityQuestionInfoView(frame: .zero)
        return view
    }()
    
    private lazy var answerView: MYGCommunityReplyQuestionView = {
        let view = MYGCommunityReplyQuestionView()
        return view
    }()
    
    private lazy var inputTextView: QMUITextView = {
        let view = QMUITextView()
        view.backgroundColor = .rgba(249, 249, 249, 1)
        view.placeholder = "请输入回答内容"
        view.font = .regular(15)
        view.placeholderColor = .rgba(153, 153, 153, 1)
        return view
    }()
    
    private lazy var sperator: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }()

    private lazy var answerButton: UIButton = {
        let btn = UIButton()
        //btn.setBackgroundColor(color: .rgba(73, 117, 236, 1), forState: .normal)
        btn.setTitle("提交", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .regular(16)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 20
        btn.addTarget(self, action: #selector(clickAnswerButton), for: .touchUpInside)
        btn.addCAGradientLayerColor(colors: [UIColor.init(hexString: "#97B3FF").cgColor,
                                                 UIColor.init(hexString: "#4975EC").cgColor],
                                        startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
        return btn
    }()
    
}

extension MYGSubmitAnswerViewController {
    @objc func clickAnswerButton(){
        
        guard let content = inputTextView.text,content.count > 0 else {
            return
        }
        MYGLoading.show()
        MMYGNetwork<MYGCommunityApi, MYGEmptyModel>.request(target: .submitAnswer(id: (model?.id)!, answer: content)) { response in
            MYGLoading.hide()
            MMYGHUD.showToast(text: "提交成功")
            
            NotificationCenter.default.post(name: NSNotification.Name.init("reloadAnswerList"), object: nil)
            self.navigationController?.popViewController(animated: true)
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
        
        
    }
    
    
}
